package com.blast.manager.net.ui

import android.content.Context
import android.content.Intent
import android.view.KeyEvent
import androidx.recyclerview.widget.LinearLayoutManager
import com.blast.manager.R
import com.blast.manager.adapter.NotMatchAdapter
import com.blast.manager.ui.ErrorHelpActivity
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.room.entity.LeiGuanEntity
import com.wxl.common.room.entity.LogEntity
import com.wxl.common.util.LayoutHelper
import com.wxl.common.util.ObservableUtil
import kotlinx.android.synthetic.main.activity_not_match_layout.*

class NotMatchActivity : AbsActivity() {

    companion object {
        fun start(context: Context, data:ArrayList<LeiGuanEntity>,isChargeError:Boolean){
            val intent = Intent(context, NotMatchActivity::class.java)
            AppContext.appContext.setLeiGuanEntity("NotMatchData",data)
            intent.putExtra("key", "NotMatchData")
            intent.putExtra("isChargeError",isChargeError)
            context.startActivity(intent)
        }
    }

    lateinit var mNotMatchAdapter: NotMatchAdapter
    var isChargeError = false
    var isBlastError = false

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_not_match_layout
    }

    override fun onCreateViewChanged() {
        isCheckAppUpgrade = false
        setTopTitle("异常列表")
        val notMatchArray = AppContext.appContext.getLeiGuanEntity(intent.getStringExtra("key")!!)
        notMatchListView.layoutManager = LinearLayoutManager(this)
        isChargeError = intent.getBooleanExtra("isChargeError", false)
        isBlastError = intent.getBooleanExtra("isBlastError",false)
        LayoutHelper.getDetonatorTitleViewsWidth(notMatchTitleLayout,object : ObservableUtil.Companion.OnNextCallback<ArrayList<Int>>(){
            override fun onNext(t: ArrayList<Int>) {
                mNotMatchAdapter = NotMatchAdapter(notMatchArray, isChargeError,isBlastError)
                mNotMatchAdapter.setWidths(t)
                mNotMatchAdapter.bindToRecyclerView(notMatchListView)
                var action = if(isBlastError) "爆破异常列表" else if(isChargeError) "充电异常列表" else "不匹配列表"
                addLog(action, "进入列表")
            }
        })

        notMatchCancel.setOnClickListener {
            finish()
        }
        notMatchSure.setOnClickListener {
            startActivity(Intent(this, ErrorHelpActivity::class.java))
        }


    }


    private fun addLog(action: String, remark: String) {
        val location = if(isBlastError) "爆破异常列表" else if(isChargeError) "充电异常列表" else "不匹配列表"
        LogEntity.addLog(
            action,
            "",
            location,
            mNotMatchAdapter.data.size,
            0.0,
            0.0,
            "",
            0,
            0,
            "",
            remark
        )
    }


    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        mNotMatchAdapter.onKeyDown(keyCode, event)
        if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_F1) {
            notMatchCancel.performClick()
            addLog("放弃", "")
        }
        if (keyCode == KeyEvent.KEYCODE_F4) {
            notMatchSure.performClick()
        }
        return true
    }
}